package com.marketlive.app.b2c.account;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/*
(C) Copyright MarketLive. 2006. All rights reserved.
MarketLive is a trademark of MarketLive, Inc.
Warning: This computer program is protected by copyright law and international treaties.
Unauthorized reproduction or distribution of this program, or any portion of it, may result
in severe civil and criminal penalties, and will be prosecuted to the maximum extent
possible under the law.
*/

import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.marketlive.app.b2c.WebUtil;
import com.marketlive.app.b2c.common.constants.ActionForwards;
import com.marketlive.app.b2c.common.constants.RequestParams;
import com.marketlive.app.b2c.common.constants.ConfigProperties;
import com.marketlive.app.b2c.struts.security.SSLSupportAction;

/**
 * Action that logs user out when logout is selected in the gateway/menu of My Account.
 * The user is logged out that is the field loggedIn in the Commerce session is set to false but the customer object in
 * the commerce session is not nulled out.
 */
public class LogoutAction extends SSLSupportAction {
    private static Log log = LogFactory.getLog(LogoutAction.class);

	/** Constant identifying current page as Logout page in the AccountRegisterForm. */
	private static final String LOGOUT_PAGE = "logout";

    /**
     * Log customer out and go to login page.
     * For details about parameters of methods please see the package description.
     */
    public ActionForward execute(ActionMapping mapping,
                                ActionForm form,
                                HttpServletRequest request,
                                HttpServletResponse response)
            throws Exception {

        log.debug("in execute() method");
        AccountRegisterForm accountRegisterForm = (AccountRegisterForm)request.getSession(false).getAttribute(RequestParams.ACCOUNT_FORM);
        accountRegisterForm.resetState();

		// make sure a new instance of CheckoutForm will be created if the user starts Checkout.
		request.getSession(false).setAttribute("checkoutForm", null);

        // set up header nav
        accountRegisterForm.setNavigationImage(ConfigProperties.LOGIN_IMAGE);
        accountRegisterForm.setNavigationMsg(LoginAction.LOGIN_IMAGE_ALT_TEXT);
        accountRegisterForm.setRequiredMsgShown(true);
        accountRegisterForm.setShowNavigation(false);
        accountRegisterForm.setCurrentPage(LOGOUT_PAGE);

        return WebUtil.findForward(request, mapping, ActionForwards.SUCCESS_KEY);
    }
}
